//
//  RecoverDataController.m
//  万物记
//
//  Created by Ajen on 2019/6/20.
//  Copyright © 2019 Ajen. All rights reserved.
//

#import "RecoverDataController.h"

@interface RecoverDataController ()
@property (weak, nonatomic) IBOutlet UITextView *dataTextView;

@end

@implementation RecoverDataController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    UILabel * titleLabel = [UILabel new];
    titleLabel.aj_Text(@"数据导入/恢复").aj_textColorHexStr(@"#2d2e36").aj_addToView(self.view);
    titleLabel.font = SongKeFont(23);
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(20);
        make.top.equalTo(self.view).offset(STATUS_BAR_HEIGHT);
        make.height.equalTo(@44);
    }];
    self.navigationItem.titleView = titleLabel;
    
    UIBarButtonItem * rightItem = [[UIBarButtonItem alloc] initWithTitle:@"导入/恢复" style:UIBarButtonItemStylePlain target:self action:@selector(rightItemDown)];
    
    rightItem.tintColor = [UIColor colorWithAjenHexString:@"#7a7374"];
    
    self.navigationItem.rightBarButtonItem= rightItem;
}

-(void)rightItemDown{
    if(self.dataTextView.text.length < 1){
        ShowTextHUD(@"请将数据粘贴到文本框内");
        return;
    }
    
    
    
    NSDictionary * jsonDic = [self dictionaryWithJsonString:self.dataTextView.text];
    if(jsonDic){
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"解析完成,是否导入/恢复到本机,点击保存继续执行\n(导入/恢复会覆盖当前所有记录,请谨慎选择)" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"保存" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
            [defaults setObject:jsonDic forKey:@"LocalJsonDic"];
            
            [LocalModel shared].model = [LocalJsonModel mj_objectWithKeyValues:[[NSUserDefaults standardUserDefaults] objectForKey:@"LocalJsonDic"]];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"REFRESH_HOME_TAB" object:nil userInfo:@{@"isEdit":@"YES"}];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popToRootViewControllerAnimated:YES];
            });
            
        }];
        [alertController addAction:cancelAction];
        [alertController addAction:okAction];
        [self presentViewController:alertController animated:YES completion:nil];
        
    }else{
        ShowTextHUD(@"解析失败,检查中途是否被篡改");
    }
    
}

- (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString
{
    
    if (jsonString == nil) {
        return nil;
    }
    
    jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\r\n" withString:@""];
    jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\t" withString:@""];
    jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\\" withString:@""];
    
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err)
    {
        NSLog(@"json解析失败：%@",err);
        ShowTextHUD(@"解析失败,检查中途是否被篡改");
        return nil;
    }
    return dic;
}
@end
